package com.tms.app.tms.ui.home.info;


import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.NotionBean;
import com.tms.app.tms.bean.TaskInfo;
import com.tms.app.tms.R;
import com.tms.app.tms.adapter.HomeAdapter;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.DTaskRes;
import com.tms.app.tms.bean.response.TaskRes;
import com.tms.app.tms.codeScan.ScanZxingActivity;
import com.tms.app.tms.db.TaskInfoDBUtil;
import com.tms.app.tms.ui.FeeInfoAC;
import com.tms.app.tms.ui.base.presenter.AbsBaseListP;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.taskinfo.TaskInfoActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by happy on 2017/8/28.
 */

public class HomeP extends AbsBaseListP {

    List<TaskInfo> list= new ArrayList<>();
    IViewHome iViewTrade;
    private int REQUEST_CODE_HOME=0x123;
    private int questType;
    private String token;
    private int planId;
    private int isHasTaskdoing;

    public void setiView(IViewHome iViewTrade) {
        this.iViewTrade = iViewTrade;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);

        if (questType==1){//执行中
            {
                DTaskRes taskRes=gson.fromJson(result,DTaskRes.class);
                sendMsgToView(taskRes);
                if (taskRes.getData()!=null&&taskRes.getData().getAllotPlanId()>0){
                    isHasTaskdoing=1;
                }else
                {
                    isHasTaskdoing=0;
                }
              //  toStartAcivity(taskRes);
            }
        }
        if (questType==2){//待执行
            TaskRes taskRes=gson.fromJson(result,TaskRes.class);
            bindDataToView(taskRes);
        }
        if (questType==3){//二维码检查
            BaseResponse baseResponse=gson.fromJson(result,BaseResponse.class);
            toStartAcivity(baseResponse);
        }
    }

    private void toStartAcivity(BaseResponse baseResponse) {
        if (baseResponse.getCode()==200){
            Intent intent=new Intent(MyApp.getInstance().getApplicationContext(),TaskInfoActivity.class);
            intent.putExtra("planId",planId);
            intent.putExtra("taskType",5);
            iViewTrade.startActivity(intent);
        }else
        {
            iViewTrade.showMsg(baseResponse.getError());
        }
    }

    @Override
    public void onFinished() {
        super.onFinished();
        if (questType==1){
            getTaskList(token);
        }
    }


    private void bindDataToView(TaskRes taskRes){
        if (iViewTrade==null)
            return;
        iViewTrade.refreshComplite();
        TaskInfoDBUtil.saveOrUpadateTask(taskRes.getData(),0);
        HomeAdapter depthAdapter= iViewTrade.getAdapter(0);
        depthAdapter.getData().clear();
        depthAdapter.getData().addAll(taskRes.getData());
//        depthAdapter.getData().addAll(TaskInfoDBUtil.getTaskList());
        depthAdapter.notifyDataSetChanged();
        if (taskRes.getData()==null||taskRes.getData().size()<1)
        {
            iViewTrade.refreshNoData();
        }else {
            iViewTrade.refreshComplite();
        }

    }






    public void getData(String token ){
        this.token=token;
        getTasking(token);
       // getTaskList(token);
    }

    private void getTasking(String token){
        questType=1;
        RequestParams params=new RequestParams(Constant.TaskDoing);
        params.addBodyParameter("token",token);
        params.addBodyParameter("showLoading", AbsServicexUtils.onlyShowLoading);
        getObjectFromServer(params);

    }

    private void getTaskList(String token){
        questType=2;
        RequestParams params=new RequestParams(Constant.TaskList);
        params.addBodyParameter("token",token);
        params.addBodyParameter("showLoading",AbsServicexUtils.onlyHideLoading);
        getObjectFromServer(params);

    }

    /****
     * 二维码检查
     * @param token
     * @param planId
     */
    public void checkPlanStatus(String token,int planId ){
        this.token=token;
        questType=3;
        this.planId=planId;
        if (planId<1)
            return;
        RequestParams params=new RequestParams(Constant.checkPlanStatus);
        params.addBodyParameter("token",token);
        params.addBodyParameter("planId",planId+"");

        getObjectFromServer(params);
    }


    @Override
    public void onLoadMoreRequested() {
        super.onLoadMoreRequested();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            TaskInfo taskInfo= (TaskInfo) adapter.getData().get(position);
            Intent intent=new Intent(view.getContext(), TaskInfoActivity.class);
            intent.putExtra("planId",taskInfo.getAllotPlanId());
            if (isHasTaskdoing==1){
                intent.putExtra("type",1);// 0,没有在执行任务，2，正在执行的任务，1已有在执行任务当前点击为待完成任务
            }else {
                intent.putExtra("type",0);// 0,没有在执行任务，2，正在执行的任务，1已有在执行任务当前点击为待完成任务
            }
            intent.putExtra("readStatus",taskInfo.getIsRead());
            iViewTrade.startActivityForResult(intent,Constant.toTaskInfo);

    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getData(token);
       // getTaskList(token);
    }



}
